package com.vipsoft.ws.config;

import com.vipsoft.ws.interceptor.AuthInterceptor;
import com.vipsoft.ws.service.IDemoWebService;
import com.vipsoft.ws.service.impl.DemoWebServiceImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Autowired
    AuthInterceptor wsAuthInterceptor;


    /**
     * 此方法作用是改变项目中服务名的前缀名，【此处127.0.0.1或者localhost不能访问时】
     * 请使用ipconfig查看本机ip来访问
     * @return
     */
    // 不能注册此类， 否者无法使用rest controller
    @SuppressWarnings("all")
    @Bean(name = "cxfServlet")
    public ServletRegistrationBean cxfServlet() {
        return new ServletRegistrationBean(new CXFServlet(), "/ws/*");
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }

    @Bean
    public IDemoWebService demoService() {
        return new DemoWebServiceImpl();
    }

    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(), demoService()); //绑定要发布的服务
        endpoint.publish("/api"); // 暴露webService api,用在资源访问
        endpoint.getInInterceptors().add(wsAuthInterceptor);
        return endpoint;
    }
}
